﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.Database.Service;

namespace T.FAS.Runtime.Expression.Dev.Dao
{
    public class ExprItemDao : DaoService<ExprItem>
    {
        public ExprItemDao() :
           base()
        {
            DBName = "TFAS";
        }

        public override string TableName => "FAS_ExprItem";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { "DataModels", "DataResultType", "Children", "ExprFuncItemParamList", "ExprExt", "ExprMetaDataAlias", "RefSchemaExpr", "RefSchemaExprValue" };
        public override Dictionary<string, object> GetDataMapping(ExprItem data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("ItemType");
            datas.Add("ItemType", (int)(data.ItemType));
            datas.Remove("ValueType");
            datas.Add("ValueType", (int)(data.ValueType));
            datas.Remove("IsLeaf");
            datas.Add("IsLeaf", (bool)data.IsLeaf);
            datas.Remove("IsDelete");
            datas.Add("IsDelete", (bool)data.IsDelete);
            return datas;
        }
        public override ExprItem ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("ItemType");
            myCol.Remove("ValueType");
            myCol.Remove("IsLeaf");
            myCol.Remove("IsDelete");
            var data = base.ConvertToObject(myCol, row);
            if (row["ItemType"] != DBNull.Value)
                data.ItemType = (ItemType)Enum.Parse(typeof(ItemType),
                                                Convert.ToString(row["ItemType"]));
            if (row["ValueType"] != DBNull.Value)
                data.ValueType = (DataType)Enum.Parse(typeof(DataType),
                                                Convert.ToString(row["ValueType"]));
            if (row["IsLeaf"] != DBNull.Value)
                data.IsLeaf = Convert.ToBoolean(row["IsLeaf"]);
            if (row["IsDelete"] != DBNull.Value)
                data.IsDelete = Convert.ToBoolean(row["IsDelete"]);
            return data;
        }

        public List<ExprItem> GetExprChildrenItemList(ExprItem exprItem, DateTime? dateTime)
        {
            var result = new List<ExprItem>();
            string sql = $"ExprMetaDataID='{exprItem.ExprMetaDataID}' and ParentID = '{exprItem.ID}'";
            if (dateTime != null)
            {
                sql += $" and LastModifyTime > '{dateTime}'";
            }
            List<object> objects = base.GetObjectsByFilter(sql);
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as ExprItem));
            }
            return result.OrderBy(t => t.SortOrder).ToList();
        }
        public List<ExprItem> GetExprItemList(string exprMetaDataID)
        {
            var result = new List<ExprItem>();
            List<object> objects = base.GetObjectsByFilter(string.Format("ExprMetaDataID='{0}'", exprMetaDataID));
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as ExprItem));
            }
            return result;
        }
    }
}
